package demo15;

import java.util.ArrayList;
import java.util.List;

public class ListDemo {
    public static void main(String[] args) {
//        定义一个List对象，用于存储书籍信息
        List<String> books = new ArrayList<>();
//        添加书籍信息
        books.add("1984");
        books.add("To kill a mockingbird");
        books.add("The great gatsby");
//        打印出书籍信息
        System.out.println("Initial list: " + books);
//        获取第一个元素
        String firstBook = books.get(0);
        System.out.println("First book in the list: " + firstBook);
//        修改第二个元素
        books.set(1, "Pride and Prejudice");
        System.out.println("List after modification: " + books);
//        删除第三个元素
        books.remove(2);
        System.out.println(("List after removal: " + books));
//        遍历List对象
        int size = books.size();
        System.out.println("Size of the list:" + size);
//        使用for循环遍历List对象
        System.out.println("Iterating through the list:");
        for (String book : books) {
            System.out.println(book);
        }
    }
}
